package com.easy.user.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.User;
import com.easy.common.service.IRegisterService;
import com.easy.common.service.IUserService;
import com.easy.common.util.PasswordUtil;
import com.easy.user.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RegisterServiceImpl extends ServiceImpl<UserMapper, User> implements IRegisterService {
    @DubboReference
    private IUserService userService;


    // 注册
    @Override
    @Transactional//注册加事务
    public User register(User user) throws Exception {
        // 检查用户名是否已存在
        User existingUser = userService.getByUsername(user.getUsername());
        if (existingUser != null) {
            throw new Exception("用户名已存在");
        }
        // 检查邮箱是否已存在
        existingUser = userService.getByEmail(user.getEmail());
        if (existingUser != null) {
            throw new Exception("邮箱已存在");
        }
        // 对密码进行加密
        String encryptedPassword = PasswordUtil.getEncryption(user.getPassword());
        System.out.println("加密后的密码111111111：" + encryptedPassword);
        user.setPassword(encryptedPassword);
        // 保存用户
        userService.addUser(user);
        System.out.println("新用户22222222："+ user);
        // 返回注册成功的用户信息（不包含密码）
        User result = userService.getUserById(user.getId());
        System.out.println("result3333333" + result);
        result.setPassword(null);
        return result;
    }
}